package com.gemme.gemme.wode;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.home.Home2Fragment;
import com.gemme.gemme.home.ShopProductInfoBean;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.view.GeAddressButton;
import com.gemme.gemme.view.GridSpacingItemDecoration;
import com.gemme.gemme.view.Topview;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

//侧滑删除地址
public class ActionActivity extends BaseActivity {

    public   String type = type_limit;
    public static final String type_limit = "time_limit_activities";//限时特价
    public static final String type_new = "discount_activities";//新品来袭
    public static final String type_buy = "full_reduction_activities";//买家推荐
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action);
        getIntentData();
        initTop();
        initView();
        initData();

    }

    private void getIntentData() {
        try {
            Intent intent = getIntent();
            String type_1 = intent.getStringExtra("type");
            if (!TextUtils.isEmpty(type_1)){
                type = type_1;
            }

        }catch (Exception e){

        }
    }

    ImageView iv_kefu;
    SwipeRefreshLayout mRefreshLayout;
    SwipeRecyclerView mRecyclerView;
    ImageView iv_picture;
    private void initView() {
        mRefreshLayout = findViewById(R.id.refresh_layout);
        iv_kefu = findViewById(R.id.iv_kefu);
        mRecyclerView = findViewById(R.id.recycler_view);
        StaggeredGridLayoutManager layoutManager = new
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        int spanCount = 2; // 3 columns
        int spacing = DpUtil.dp2px(this,12); // 50px
        boolean includeEdge = false;
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, includeEdge));

        mRefreshLayout.setEnabled(false);



        iv_picture = findViewById(R.id.iv_picture);
        findViewById(R.id.iv_return_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRecyclerView.scrollToPosition(0);
            }
        });
        jumpKefu();
    }

    private void jumpKefu() {
        iv_kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ChatClient.getInstance().isLoggedInBefore()){
                    //已经登录，可以直接进入会话界面
                    System.out.println();
                    Intent intent = new IntentBuilder(ActionActivity.this)
                            .setServiceIMNumber("gemmekefu") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                            .build();
                    startActivity(intent);
                }else{
                    //未登录，需要登录后，再进入会话界面
                    System.out.println();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }




    public class ActionAdapter extends RecyclerView.Adapter<ActionAdapter.ViewHolder>{

        private List<ShopProductInfoBean.DataBean.NewObjBean> dataList;

        public ActionAdapter(List<ShopProductInfoBean.DataBean.NewObjBean> dataList){

            this.dataList = dataList;
        }
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.action_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new ActionAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            try {
//                holder.picture.setText("");
                ShopProductInfoBean.DataBean.NewObjBean newObjBean = dataList.get(position);

                Glide.with(mContext).load(newObjBean.getAbbreviationPic()).into(holder.picture);
                holder.name.setText("" + newObjBean.getProductName());
                holder.tv_desc.setText("" + newObjBean.getProductDesc());
                holder.tv_price.setText("" + newObjBean.getPrice());

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext, ProductNewActivity.class);
                        intent.putExtra("id",newObjBean.getProductId());
                        intent.putExtra("brandId",newObjBean.getBrandId() + 0L);
                        mContext.startActivity(intent);
                    }
                });

            }catch (Exception e){

            }
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView picture;
            TextView name;
            TextView tv_desc;
            TextView tv_price;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                picture = itemView.findViewById(R.id.picture);
                name = itemView.findViewById(R.id.name);
                tv_desc = itemView.findViewById(R.id.tv_desc);
                tv_price = itemView.findViewById(R.id.tv_price);
            }
        }
    }




    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("限时特价");
    }

    private void initData() {
//        http://192.168.1.9:9999/mall/shopproductinfo/page?current=1&size=10&activityCode=discount_activities
        String url = DataManager.BaseUrl_weicheng + "mall/shopproductinfo/page?current=1&size=10"+
                "&activityCode=" + type;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer3(this, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    ShopProductInfoBean shopProductInfoBean = JSON.parseObject(response, ShopProductInfoBean.class);
                    List<ShopProductInfoBean.DataBean> data = shopProductInfoBean.getData();
                    List<ShopProductInfoBean.DataBean.NewObjBean> newObj = data.get(0).getNewObj();

                    mRecyclerView.setAdapter(new ActionAdapter(newObj));

                    String actPic = data.get(0).getActPic();
                    Glide.with(mContext).load(actPic).into(iv_picture);
                }catch (Exception e){

                }
            }
        });
    }


}